package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.Chess;
import model.ChessArray;
import model.Prompt;
import util.SocketUtil;

public class GridPanel extends JPanel implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Prompt prompt;
	private ChessArray chessArray;
	private Image blackchess, whitechess;
	private int chessw, chessh, left, top;

	public GridPanel() {
		setBackground(new Color(0xB2DFEE));
		setOpaque(false);
		prompt=new Prompt();
		prompt.setPrompt("请点击开始", 48, Color.black, 1f);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if(chessArray.isMy()==false)
					return;
				int j=(e.getX()-left)/chessw;
				int i=(e.getY()-top)/chessh;
				Chess chess=new Chess();
				chess.setX(j);
				chess.setY(i);
				chess.setColor(SocketUtil.getColor());
				if(chessArray.SaveChess(chess)){
					SocketUtil.SendMessage(chess);
					repaint();
				}
			}
		});
		blackchess = new ImageIcon(getClass().getResource("../images/blackChessman.png")).getImage();
		whitechess = new ImageIcon(getClass().getResource("../images/whiteChessman.png")).getImage();

	}
	public GridPanel(ChessArray chessArray){
		this();
		this.chessArray=chessArray;
	}



	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite composite = g2.getComposite();
		g2.setColor(new Color(0xB2DFEE));
		g2.setComposite(AlphaComposite.SrcOver.derive(0.9f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(composite);
		drawGrid(g2);
		drawChess(g2);
		drawPrompt(g2, prompt);
	}

	private void drawGrid(Graphics2D g2) {
		int width = getWidth();
		int height = getHeight();
		// System.out.println(height);//488
		// System.out.println(width);//474
		chessw = width / 14;
		chessh = height / 14;
		left = width % 14 / 2;
		top = height % 14 / 2;
		int x = left + chessw / 2, y = top + chessh / 2;
		g2.setColor(Color.black);
		for (int i = 0; i < 15; i++) {
			g2.drawLine(x + i * chessw, y, x + i * chessw, height - y);
			g2.drawLine(x, y + i * chessh, width - x, y + i * chessh);
		}
	}

	private void drawChess(Graphics2D g) {
		for (int i = 0; i < ChessArray.ROWS; i++)
			for (int j = 0; j < ChessArray.COLUMNS; j++) {
				if (chessArray.getChess(i, j) != null) {
					Chess chess = chessArray.getChess(i, j);
					if (chess.equalColor(Color.black))
						g.drawImage(blackchess, chess.getX() * chessw + left, chess.getY() * chessh + top, chessw,
								chessh, this);
					else
						g.drawImage(whitechess, chess.getX() * chessw + left, chess.getY() * chessh + top, chessw,
								chessh, this);
				}
			}
	}
	private void drawPrompt(Graphics2D g,Prompt prompt){
		Composite composite=g.getComposite();
		g.setColor(prompt.getColor());
		g.setComposite(AlphaComposite.SrcOver.derive(prompt.getAlpha()));
		Font font=new Font("幼圆", Font.TRUETYPE_FONT, prompt.getFontsize());
		g.setFont(font);
		FontRenderContext frc=g.getFontRenderContext();
		Rectangle2D rt=font.getStringBounds(prompt.getMessage(), frc);
		g.drawString(prompt.getMessage(), (int) (getWidth() - rt.getWidth()) / 2,
				(int) (getHeight() - rt.getHeight()) / 2);
		g.setComposite(composite);
	}
	public Prompt getPrompt() {
		return prompt;
	}

	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof Chess){
			Chess chess=(Chess) arg;
			chessArray.SaveChess(chess);
			repaint();
		}
	}
}
